
package com.myopen.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.converter.ListConverter;
import com.myopen.kit.UUIDKit;

public class Order extends Model<Order>{

	private static final long serialVersionUID = -6408995522291184390L;
	public static final Order dao=new Order();
	
	public Page<Record> pagequery(Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_shop_order t "
				+ " left join t_user u on t.userId=u.id"
				+ " where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		if(StrKit.notBlank(record.getStr("status"))){
			sqlExceptSelect.append(" and t.status = ? ");
			list.add(record.getStr("status"));
		}

		sqlExceptSelect.append(" order by t.createTime desc");

		String sqlSelect="select t.* ";
		Page<Record> page=Db.paginate(record.getInt("pageNumber"),record.getInt("pageSize"),sqlSelect,sqlExceptSelect.toString(),list.toArray());		
   
		String[] dicTypeKeys= {"status"};
		String[] dicTypes= {"SHOP_ORDER_STATUS"};
		return new ListConverter<Record>(page,dicTypeKeys,dicTypes).getPage();
    }
	
	public Record getRecordById(String id){
		String sql="select t.* "+
		" from t_shop_order t "+
		" where t.id=? ";
		Record order= Db.findFirst(sql,id);
		
		return order;
	}

	public Record getPayHistory(String out_trade_no){
		System.out.println("=======out_trade_no:"+out_trade_no);
		String sql="select * from t_shop_pay_history t where t.out_trade_no=?  ";
		Record payHis=Db.findFirst(sql,out_trade_no);
		return payHis;
	}

	public Record getPayHistorySuccessByOrderId(String orderId){
		String sql="select * from t_shop_pay_history t where  t.payStatus='1' and t.orderId=?  ";
		return Db.findFirst(sql,orderId);
	}

	/**
	 * 确认单名单
	 * @param orderId
	 * @param userId
	 */
	public void verify(String orderId,String userId){
		String sql="update t_shop_order t set t.status='3' where id=? ";
		Db.update(sql,orderId);
	}

	/**
	 * 发货
	 * @param orderId
	 * @param userId
	 */
	public void delivery(String orderId,String userId){
		String sql="update t_shop_order t set t.status='4' where id=? ";
		Db.update(sql,orderId);
	}

	/**
	 * 取消订单
	 */
	public void cancel(String orderId,String userId){
		String sql="update t_shop_order t set t.status='6' where id=? ";
		Db.update(sql,orderId);
	}

	/**
	 * 退款状态修改
	 * @param orderId
	 * @param userId
	 */
	public void refund(String orderId,String userId){
		String sql="update t_shop_order t set t.status='99' where id=? ";
		Db.update(sql,orderId);
	}

	/**
	 * 订单完成
	 * @param orderId
	 * @param userId
	 */
	public void complete(String orderId,String userId){
		String sql="update t_shop_order t set t.status='100' where id=? ";
		Db.update(sql,orderId);
	}

	/**
	 * 创建支付记录
	 * orderId订单ID
	 * trade_no支付流水号
	 * amount金额
	 * payType支付类型。 支付方式，0支付宝，1微信，2线下支付
	 * payTypeSub 具体支付类型下的，某种支付渠道支付类型，包含了支付宝手机网网页支付，PC网页支付，微信扫码支付，微信H5支付，微信公众号支付等，保留原代码
	 */
	public String createPayHistory(String orderId,String out_trade_no,Double amount,String payType,String payTypeSub){

		Record r=null;

		String sql="select * from t_shop_pay_history t where t.orderId=?";
		r=Db.findFirst(sql,orderId);

		if(r==null){
			r=new Record();
			r.set("id", UUIDKit.getUUID());
			r.set("removed", "0");
			r.set("createTime", new Date());
			r.set("updateTime", new Date());
			r.set("orderId", orderId);
			r.set("out_trade_no", out_trade_no);
			r.set("payStatus", "0");
			r.set("amount", amount);
			r.set("payType", payType);
			r.set("payTypeSub", payTypeSub);

			Db.save("t_shop_pay_history", r);
		}else{
			r.set("updateTime", new Date());
			r.set("payType", payType);
			r.set("payTypeSub", payTypeSub);

			Db.update("t_shop_pay_history", r);
		}

		return "0";
	}

	/**
	 * 支付完成
	 * orderId订单ID
	 * trade_no,第三方支付订单ID，可用于退款
	 */
	public String successPayHistory(String out_trade_no,String trade_no){
		Record payHis=getPayHistory(out_trade_no);

		String orderId=payHis.getStr("orderId");
		Order order=Order.dao.findById(orderId);
		order.set("status", "2");
		order.update();

		payHis.set("payStatus", "1");
		payHis.set("trade_no", trade_no);
		payHis.set("paySuccessTime", new Date());
		Db.update("t_shop_pay_history",payHis);
		
		return "0";

	}

	public String completePayHistory(String out_trade_no,String trade_no){
		Record payHis=getPayHistory(out_trade_no);
		
		payHis.set("payStatus", "3");
		payHis.set("payCompleteTime", new Date());
		Db.update("t_shop_pay_history",payHis);
		
		return "0";
	}
    

}
